package com.vhans.db.service.impl;

import com.vhans.core.utils.BeanCopyUtils;
import com.vhans.core.web.model.dto.AgreementDTO;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.vo.agreement.AgreementAllVO;
import com.vhans.db.model.vo.agreement.AgreementCenterVO;
import com.vhans.db.model.vo.agreement.AgreementHomeListVO;
import com.vhans.db.model.vo.agreement.AgreementInfoAllVO;
import com.vhans.db.service.*;
import nl.basjes.shaded.org.springframework.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.vhans.core.enums.FilePathEnum.AGREEMENTI;

/**
 * 聚合约起业务处理
 *
 * @author vhans
 */
@Service
public class BAgreementServiceImpl implements IBAgreementService {

    @Autowired
    private IBAppointmentService appointmentService;

    @Autowired
    private IBActivityService activityService;

    @Autowired
    private IBCompetitionService competitionService;

    @Autowired
    private IBHelpService helpService;

    @Autowired
    private IAFileService fileService;

    private final Integer[] typeList = {1, 2, 3, 4};

    /**
     * 查看约起列表
     *
     * @param condition 条件
     * @return 约起列表
     */
    public List<AgreementAllVO> listAgreementVO(ConditionDTO condition) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(condition.getType()),
                "未传入类型或者类型不正确");
        return switch (condition.getType()) {
            case 1 -> BeanCopyUtils.copyBeanList(appointmentService.listAppointmentVO(condition), AgreementAllVO.class);
            case 2 -> BeanCopyUtils.copyBeanList(activityService.listActivityVO(condition), AgreementAllVO.class);
            case 3 -> BeanCopyUtils.copyBeanList(competitionService.listCompetitionVO(condition), AgreementAllVO.class);
            case 4 -> BeanCopyUtils.copyBeanList(helpService.listHelpVO(condition), AgreementAllVO.class);
            default -> Collections.emptyList();
        };
    }

    @Override
    public void addAgreement(AgreementDTO agreement, Integer type) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(type),
                "未传入类型或者类型不正确");
        switch (type) {
            case 1 -> appointmentService.addAppointment(agreement);
            case 2 -> activityService.addActivity(agreement);
            case 3 -> competitionService.addCompetition(agreement);
            case 4 -> helpService.addHelp(agreement);
        }
    }

    @Override
    public void deleteAgreement(AgreementDTO agreement) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(agreement.getAgreementType()),
                "未传入类型或者类型不正确");
        switch (agreement.getAgreementType()) {
            case 1 -> appointmentService.deleteAppointment(agreement.getId());
            case 2 -> activityService.deleteActivity(agreement.getId());
            case 3 -> competitionService.deleteCompetition(agreement.getId());
            case 4 -> helpService.deleteHelp(agreement.getId());
        }
    }

    @Override
    public String updateAgreement(AgreementDTO agreement, Integer type) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(type),
                "未传入类型或者类型不正确");
        return switch (type) {
            case 1 -> appointmentService.updateAppointment(agreement);
            case 2 -> activityService.updateActivity(agreement);
            case 3 -> competitionService.updateCompetition(agreement);
            case 4 -> helpService.updateHelp(agreement);
            default -> "更改失败";
        };

    }

    @Override
    public AgreementInfoAllVO editAgreement(AgreementDTO agreement) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(agreement.getAgreementType()),
                "未传入类型或者类型不正确");
        return switch (agreement.getAgreementType()) {
            case 1 -> BeanCopyUtils.copyBean(appointmentService.editAppointment(agreement.getId()), AgreementInfoAllVO.class);
            case 2 -> BeanCopyUtils.copyBean(activityService.editActivity(agreement.getId()), AgreementInfoAllVO.class);
            case 3 -> BeanCopyUtils.copyBean(competitionService.editCompetition(agreement.getId()), AgreementInfoAllVO.class);
            case 4 -> BeanCopyUtils.copyBean(helpService.editHelp(agreement.getId()), AgreementInfoAllVO.class);
            default -> null;
        };
    }

    @Override
    public List<AgreementHomeListVO> listAgreementHomeVO(Integer type) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(type),
                "未传入类型或者类型不正确");
        return switch (type) {
            case 1 -> appointmentService.listAppointmentHomeVO();
            case 2 -> activityService.listActivityHomeVO();
            case 3 -> competitionService.listCompetitionHomeVO();
            case 4 -> helpService.listHelpHomeVO();
            default -> Collections.emptyList();
        };
    }

    @Override
    public AgreementCenterVO getAgreementAgreementById(AgreementDTO agreement) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(agreement.getAgreementType()) ||
                        agreement.getAgreementType() == 6,
                "未传入类型或者类型不正确");
        return switch (agreement.getAgreementType()) {
            case 1 -> appointmentService.getAppointmentAgreementById(agreement.getId());
            case 2 -> activityService.getActivityAgreementById(agreement.getId());
            case 3 -> competitionService.getCompetitionHomeById(agreement.getId());
            case 4 -> helpService.getHelpAgreementById(agreement.getId());
            case 6 -> appointmentService.getSpecifyAppointmentAgreementById(agreement.getId());
            default -> null;
        };
    }

    @Override
    public String joinAgreement(AgreementDTO agreement) {
        Assert.isTrue(Arrays.stream(typeList).toList().contains(agreement.getAgreementType()) ||
                        agreement.getAgreementType() == 5 || agreement.getAgreementType() == 6,
                "未传入类型或者类型不正确");
        return switch (agreement.getAgreementType()) {
            case 1 -> appointmentService.answerAppointment(agreement.getId());
            case 2 -> activityService.participationActivity(agreement.getId());
            case 3 -> competitionService.takeCompetition(agreement.getId());
            case 4 -> helpService.participationHelp(agreement.getId());
            case 5 -> competitionService.applyCompetition(agreement.getId());
            case 6 -> appointmentService.uniqueAppointment(agreement.getId());
            default -> "加入失败";
        };
    }

    @Override
    public String saveAgreementImages(MultipartFile file) {
        // 上传图片
        return fileService.uploadStrategyFile(file, AGREEMENTI.getPath(), "image");
    }
}
